package com.bang.controller;

import com.bang.entity.Admin;
import com.bang.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Admin)表控制层
 *
 * @author makejava
 * @since 2020-05-16 13:18:57
 */
@Controller
public class AdminController {
    /**
     * 服务对象
     */
    @Autowired
    private AdminService adminService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Admin selectOne(Integer id) {
        return this.adminService.queryById(id);
    }

    @RequestMapping("/admins")
    public String list(Model model){
        List<Admin> admins=adminService.queryAll();
        model.addAttribute("admins",admins);
        return "emp/admins";
    }

    @GetMapping("/toaddadmin")
    public String toAdd(Model model){
        return "emp/addAdmin";
    }

    @PostMapping("/addadmin")
    public String AddEmployee(Admin admin,Model model){
        adminService.insert(admin);
        return "redirect:/admins";
    }

    @GetMapping("/toupdateadmin/{id}")
    public String toUpdate(@PathVariable("id")Integer id,Model model){

        Admin admin = adminService.queryById(id);
        model.addAttribute("admin",admin);

        return "emp/updateAdmin";
    }

    @PostMapping("/updateadmin")
    public String updateEmp(Admin admin){
        adminService.update(admin);
        return "redirect:/admins";
    }

    @RequestMapping("/deleteadmin/{id}")
    public String delete(@PathVariable("id")int id){
        adminService.deleteById(id);
        return "redirect:/admins";
    }

}